<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>x-admin</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/xadmin/lib/layui/css/layui.css" media="all">
</head>
<body>
<div class="layui-card">
    <div class="layui-card-body">
        <!--主体内容 start-->
        <div class="layui-row">

            <div class="layui-col-md3">
                <input type="text" id="ser_row" name="ser_row" required  lay-verify="required" placeholder="请输入顶级名称" autocomplete="off" class="layui-input">
            </div>

            <div class="layui-col-md1">
                <button style="margin-left: 50px;" class="layui-btn" onclick="ser_on()">搜索</button>
            </div>
            <div class="layui-col-md8">
                <button style="float: right;" class="layui-btn" onclick="ser_add()">添加</button>
            </div>
        </div>
        <table id="demo" lay-filter="test">

        </table>


        <!--主体内容 end-->
    </div>
</div>
<script src="/static/xadmin/lib/layui/jquery-3.2.1.min.js"></script>
<script src="/static/xadmin/lib/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/xadmin/' //静态资源所在路径
    });
    layui.use(['laydate','form'], function(){
        var laydate = layui.laydate;
        var table = layui.table;
        var form = layui.form;
    });
    layui.use('table', function(){
        var table = layui.table;
        var urls = '{:url("/admin/articlecate/index")}';
        //第一个实例
        TB = table.render({
            elem: '#demo'
            ,height: 'full-100'
            ,limits:[100,500]
            ,limit:100
            ,url: urls //数据接口
            ,page: true //开启分页
            ,cols: [[ //表头
                {field: 'id', title: 'ID'}
                ,{field: 'cate_name', title: '分类名称',templet:function (d) {
                    return '<span>'+d.lefthtml+''+d.cate_name+'</span>'
                }}
                ,{field: 'sort', title: '排序',templet:function (d) {
                    if(d.parent_id==0){
                        return '<span style="color: red">'+d.sort+'</span>'
                    }else {
                        return '<span>'+d.sort+'</span>'
                    }

                }}
                ,{field: '#', title: '操作',toolbar: '#barDemo'}
            ]]
        });
    });
    //搜索
    function ser_on(){
        var ser_row = $("#ser_row").val();
        var urls = '{:url("admin/articlecate/index")}'+'?keyword='+ser_row;
        TB.reload({
            url: urls
            ,cols: [[ //表头
                {field: 'id', title: 'ID'}
                ,{field: 'cate_name', title: '分类名称',templet:function (d) {
                    return '<span>'+d.lefthtml+''+d.cate_name+'</span>'
                }}
                ,{field: 'sort', title: '排序',templet:function (d) {
                    if(d.parent_id==0){
                        return '<span style="color: red">'+d.sort+'</span>'
                    }else {
                        return '<span>'+d.sort+'</span>'
                    }

                }}
                ,{field: '#', title: '操作',toolbar: '#barDemo'}
            ]]
        });
    }
    //    添加
    function ser_add() {
        var url='{:url("admin/articlecate/add")}';
        layer.open({
            type: 2,
            title: '信息',
            shadeClose: true,
            shade: 0.8,
            area: ['80%', '80%'],
            content: url, //iframe的url
            end:function () {
                location.reload()
            }
        });
    }
    //    b编辑
    function set_edit(t) {
        var id=$(t).attr("id-data");
        var url='{:url("admin/articlecate/edit")}'+'?id='+id;
        layer.open({
            type: 2,
            title: '信息',
            shadeClose: true,
            shade: 0.8,
            area: ['80%', '80%'],
            content: url, //iframe的url
            end:function () {
                location.reload()
            }
        });
    }


    //删除
    function set_del(t) {
        var id=$(t).attr("id-data");
        layer.confirm('你确定要删除么？', {
            btn: ['确定', '取消'] //可以无限个按钮
        }, function(index, layero){
            //按钮【按钮一】的回调
            $.post('{:url("admin/articlecate/del")}',{'id':id},function(data){
                if(data.code==1){
                    layer.msg(data.msg,{icon:1});
                    setTimeout(function () {
                        location.reload();
                    },1500);

                }else{
                    layer.alert(data.msg,{icon:2});
                }
            });

        }, function(index){
            //按钮【按钮二】的回调
        });
    }

</script>
</body>
</html>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="detail"  id-data="{{d.id}}" onclick="set_edit(this)">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs "  id-data="{{d.id}}" onclick="set_del(this)">删除</a>
</script>
